import { WebGLUtil } from "../../utils/WebGLUtil";
import { IProgramInfo } from "../interface/IDefine";
import { Render } from "../Render";
import { Skin } from "./Skin";

/*
 * @Author: Snow
 * @Date: 2021-10-21 11:54:47
 * @Description: 蒙皮渲染
 */
export class SkinRenderer {
    public mesh:any;

    public skin:Skin;

    public skinProgramInfo:IProgramInfo;
    constructor(mesh, skin,skinProgramInfo:IProgramInfo) {
        this.mesh = mesh;
        this.skin = skin;
        this.skinProgramInfo = skinProgramInfo;
    }
    render(node, projection, view, sharedUniforms) {
        const gl = Render.GL;
        const { skin, mesh } = this;
        skin.update(node);
        gl.useProgram(this.skinProgramInfo.program);
        for (const primitive of mesh.primitives) {
            WebGLUtil.setBuffersAndAttributes(gl, this.skinProgramInfo, primitive.bufferInfo);
            WebGLUtil.setUniforms(this.skinProgramInfo, {
                u_projection: projection,
                u_view: view,
                u_world: node.worldMatrix,
                u_jointTexture: skin.jointTexture,
                u_numJoints: skin.joints.length,
            });
            WebGLUtil.setUniforms(this.skinProgramInfo, primitive.material.uniforms);
            WebGLUtil.setUniforms(this.skinProgramInfo, sharedUniforms);
            WebGLUtil.drawBufferInfo(gl, primitive.bufferInfo);
        }
    }
}